public class Solution75 {
    public void sortColors(int[] nums) {
        int one = -1, two = -1;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                if (one >= 0) {
                    swap(nums, one, i);
                    one++;
                }
                if (two >= 0) {
                    swap(nums, two, i);
                    two++;
                }
            } else if (nums[i] == 1) {
                if (two >= 0) {
                    swap(nums, two, i);
                    two++;
                    if (one < 0) {
                        one = two - 1;
                    }
                } else {
                    if (one < 0) {
                        one = i;
                    }
                }
            } else {
                if (two < 0) {
                    two = i;
                }
            }
        }
    }

    private void swap(int[] nums, int i, int j) {
        int t = nums[i];
        nums[i] = nums[j];
        nums[j] = t;
    }
}
